<?php
namespace Home\Controller;

class AdController extends HomeController {
    
    public function _initialize(){
        C('SHOW_PAGE_TRACE',false);
    }
    
    /**
     * @todo 通过js引用方式，获取广告信息
     * @example <div id="ad_1"></div><div id="ad_2"></div>
     *          <script src="{:U('Ad/index',array('id'=>'1,2'))}"></script>
     * @author fzh
     */
    public function index() {
        $id = trim((string)I('get.id'));
        if(!preg_match('/^[\d,]*$/', $id)){
            return ;
        }
        $postion_id = explode(',', $id);
        $ad = D('Ad')->getAd($postion_id);
        foreach ($ad as $key=>$val){
            echo '$("#ad_'.$key.'").html( \''.$val.'\');';
        }
    }
    
    
    /**
     * @todo 自定义模板样式调取（只允许调取一个位置的广告信息）
     * @example <div id="ad_1"></div>
     *          <script src="{:U('Ad/index',array('id'=>'1'))}"></script>
     * @author fzh
     */
    function custom(){
        $id = (int)I('get.id');
        if($id == 0){
            return ;
        }
        $postion_id = (array)$id;
        $obj = D('Admin/ServiceApi');
        //获取位置信息
        $postion_info = $obj->getAdPostion($postion_id);
        if(!isset($postion_info[$id])){
            return ;
        }
        $limit = (int)$postion_info[$id]['ad_position_limit'];
        $template = $postion_info[$id]['ad_position_template'];
        $type = $postion_info[$id]['ad_position_type'];
        $this->assign('template',$template);
        
        //非自定义广告
        if($type != 1){
            $this->assign('list',array());
        }else{
            //获取广告信息
            $ad_list = $obj->getAdList($postion_id,$limit);
            if(!isset($ad_list[$id])){
                return ;
            }
            $this->assign('list',$ad_list[$id]);
        }
        $content = $this->fetch('custom');
        $this->show($content);
    }
}